<?php
/**
 * Created by PhpStorm.
 * User: Damon
 * Date: 2017/7/3
 * Time: 11:37
 */
namespace Home\Controller;
use Core\Controller;
use Core\DB;
class IndexController extends Controller{
    public function index(){
        $this->display("index.tpl");
    }
    public function welcome(){
        $this->display("welcome.tpl");
    }
    public function input(){
        $this->display("input.tpl");
    }
    public function main(){
        $this->display("main.tpl");
    }
    public function userinfo(){
            $title = "用户资产统计";
            $th = array(
                'id'=>"序号",
                'type'=>"类型",
                'content'=>"统计记录",);
            $data = $list =array();
            $username = trim($_POST['username']);
            $starttime = isset($_POST['starttime'])?trim($_POST['starttime']):null;
            $endtime = isset($_POST['endtime'])?trim($_POST['endtime']):null;
            $userid = $this->getuserid($username)['id'];
            if(!$userid){
                $result = create_label("用户不存在!");
                echo(json_encode($result));exit;
            }
            if (isset($userid) && $userid) {
                $account = $this->getaccount($userid);
                $finan_pay = $this->finan_pay($userid, $starttime, $endtime);
                $finan_out = $this->finan_out($userid, $starttime, $endtime);
                $coin_in = $this->coin_in($userid, $starttime, $endtime);
                $coin_out = $this->coin_out($userid, $starttime, $endtime);
                $trade_buy = $this->trade_buy($userid, $starttime, $endtime);
                $trade_sell = $this->trade_sell($userid, $starttime, $endtime);
                $dongjie = $this->dongjie($userid, $starttime, $endtime);
                $money = $finan_pay['money'] - $finan_out['money'] - $trade_buy['money'] + $trade_sell['money'] + $dongjie['money'];
                $dongjiemoney = $dongjie['money'];
                $dongjixbc = $dongjie['xbc'];
                $xbc = $coin_in['xbc'] + $trade_buy['xbc'] - $coin_out['xbc'] - $trade_sell['xbc'] + $dongjie['xbc']; $time = '';
                if ($starttime) {
                    $time .= '起始时间:' . $starttime;
                }
                if ($endtime) {
                    $time .= '  结束时间:' . $endtime;
                }
                $list['time'] = ($time != '') ? $time : '默认系统初始时间至当天';
                $list['info'] = "用户$username" . "账号资产:<br>";
                $list['info'] .= "人民币：." . $account[1]['balance_amount'] . "冻结有" . $account[1]['freeze_amount'] . "<br>";
                $list['info'] .= "喜宝币：." . $account[2]['balance_amount'] . "冻结有" . $account[2]['freeze_amount'] . "<br>";
                $list['info'] .= "数宝币：." . $account[3]['balance_amount'] . "冻结有" . $account[3]['freeze_amount'] . "<br>";
                $list['info'] .= "聚宝币：." . $account[4]['balance_amount'] . "冻结有" . $account[4]['freeze_amount'] . "<br>";
                $list['info'] .= "财宝币：." . $account[5]['balance_amount'] . "冻结有" . $account[5]['freeze_amount'] . "<br>";

                $list['info'] .= "资金变化为：$money";
                if ($dongjiemoney > 0) {
                    $list['info'] .= ",其中冻结有冻结金额：$dongjiemoney";
                }
                $list['info'] .= "。喜宝币变化为：$xbc";
                if ($dongjixbc > 0) {
                    $list['info'] .= ",其中冻结有数量：$dongjixbc";
                }
                $list['info'] .= "。";
                $list['finan'] = '充值' . $finan_pay['money'] . '   提现' . $finan_out['money'] . '   共入账人民币' . ($finan_pay['money'] - $finan_out['money']) . "其中提现金额有" . $finan_out['dmoney']; //提现 ￥-
                $list['trade'] = '买入' . $trade_buy['xbc'] . '   花费人民币' . $trade_buy['money'] . "<br>卖出" . $trade_sell['xbc'] . '   入账人民币' . $trade_sell['money']; //卖出喜宝
                $list['coin'] = '转入' . $coin_in['xbc'] . '   转出' . $coin_out['xbc'];
                $i = 1;
                foreach ($list as $key => $value) {
                    $arr['id'] = $i++;
                    switch ($key) {
                        case 'time' :
                            $arr['type'] = '时间';
                            break;
                        case 'info' :
                            $arr['type'] = '用户资产';
                            break;
                        case 'finan' :
                            $arr['type'] = '充值提现';
                            break;
                        case 'trade' :
                            $arr['type'] = '交易买卖';
                            break;
                        case 'coin' :
                            $arr['type'] = '转入转出';
                            break;
                    }
                    $arr['content'] = $value;
                    $data[] = $arr;
                }
            }
            $result = create_table($title,$th,$data);
            echo(json_encode($result));exit;
    }
    public function getuserid($username=''){
        $where['username'] = $username;
        return DB::table('user')->where($where)->find();
    }
    public function getaccount($userid =1120){
        $result = array();
        $where['user_id'] = $userid;
        $data =  DB::table('account')->field('coin_id,balance_amount,freeze_amount')->where($where)->select();
        if($data){
            foreach($data as $value){
                $result[$value['coin_id']] = $value;
            }
        }
        return $result;
    }
    //人民币充值 ￥+
    public function finan_pay($userid =1120,$addtime =null,$endtime=null){
        $where['userid'] = $userid;
        $where["status#in"] = [1,2];
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $num = DB::table("finanpay")->where($where)->sum('mum');
        $datas['money'] = $num;
        return $datas;
    }
    //人民币提现 ￥-  fee
    public function finan_out($userid =1120,$addtime =null,$endtime=null){
        $where['user_id'] = $userid;
        $where["status"] = 2;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $money = DB::table("withdrawals")->where($where)->sum('num');
        $fee = DB::table("withdrawals")->where($where)->sum('fee');
        $datas['money'] = $money+$fee;
        $datas['fee'] = $fee;
        $where["status"] = 0;
        $money1 = DB::table("withdrawals")->where($where)->sum('num');
        $fee1 = DB::table("withdrawals")->where($where)->sum('fee');
        $datas['dmoney'] = $money1+$fee1;
        return $datas;
    }

    //人民币购买 ￥- $+ fee
    public function trade_buy($userid =1120,$addtime =null,$endtime=null){
        $datas = [];
        $where['turnover_status'] = 1;
        $where['entrust_buyuserid'] = $userid;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }

        $trade = DB::table("turnoverorder")->where($where)->sum("turnover_amount");
        $fee = DB::table("turnoverorder")->where($where)->sum("turnover_buyfee");
        $where['market_id'] = 1;
        $xbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 2;
        $sbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 3;
        $jbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 4;
        $cbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $datas['money'] = $trade+$fee;
        $datas['fee'] = $fee;
        $datas['xbc'] = $xbc;
        $datas['sbc'] = $sbc;
        $datas['jbc'] = $jbc;
        $datas['cbc'] = $cbc;
        return $datas;
    }
    //积分卖出 ￥+ $- fee
    public function trade_sell($userid = 1120,$addtime =null,$endtime=null){
        $datas = [];
        $where['turnover_status'] = 1;
        $where['entrust_selluserid'] = $userid;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $trade = DB::table("turnoverorder")->where($where)->sum("turnover_amount");
        $fee = DB::table("turnoverorder")->where($where)->sum("turnover_buyfee");
        $where['market_id'] = 1;
        $xbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 2;
        $sbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 3;
        $jbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $where['market_id'] = 4;
        $cbc = DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $datas['money'] = $trade-$fee;
        $datas['fee'] = $fee;
        $datas['xbc'] = $xbc;
        $datas['sbc'] = $sbc;
        $datas['jbc'] = $jbc;
        $datas['cbc'] = $cbc;
        return $datas;
    }
    //冻结 ￥- $-
    public function dongjie($userid = 1120,$addtime =null,$endtime=null){
        $datas =[];
        $where['user_id'] = $userid;
        $where['status'] = 0;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $trade = DB::table("entrustorder")->where($where)->sum("freeze");
        $where['market_id'] = 1;
        $xbc = DB::table("entrustorder")->where($where)->sum("freeze");
        $where['market_id'] = 2;
        $sbc = DB::table("entrustorder")->where($where)->sum("freeze");
        $where['market_id'] = 3;
        $jbc = DB::table("entrustorder")->where($where)->sum("freeze");
        $where['market_id'] = 4;
        $cbc = DB::table("entrustorder")->where($where)->sum("freeze");
        $datas['money'] = $trade;
        $datas['xbc'] = $xbc;
        $datas['sbc'] = $sbc;
        $datas['jbc'] = $jbc;
        $datas['cbc'] = $cbc;
        return $datas;
    }

    //转入 ￥+ $+
    public  function coin_in($userid = 1120,$addtime =null,$endtime=null){
        $datas =[];
        $where['status'] = 1;
        $where['coinname'] = 'xbc';
        $where['userid'] = $userid;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $datas['xbc'] = DB::table("inwallet")->where($where)->sum("num");
        return $datas;
    }
    //转出 ￥- $- fee
    public  function coin_out($userid = 1120,$addtime =null,$endtime=null){
        $datas =[];
        $where['status'] = 1;
        $where['coinname'] = 'xbc';
        $where['userid'] = $userid;
        if($addtime){
            $where['create_time#>='] = $addtime;
        }
        if($endtime){
            $where['create_time#<='] = $endtime;
        }
        $datas['xbc'] = DB::table("outwallet")->where($where)->sum("num");
        return $datas;
    }
}